<?php

namespace app\admin\controller;

use app\admin\BaseController;

class OrganizationController extends BaseController
{
    /**
     * 获取商品列表
     * @return \think\response\Json
     */
    public function listsAction()
    {
        try{
            $post = $this->get;

            $params['where'][] = ['appletId', '=', $this->appletId];

            $params['order'] = ['organizationId'=>'desc'];

            if(!empty($post['organizationName'])){
                $params['where'][] = ['organizationName','like','%'.$post['organizationName'].'%'];
            }

            if(!empty($post['categoryId'])){
                $params['where'][] = ['categoryId', '=', $post['categoryId']];
            }

            if(!empty($post['provinceCode'])){
                $params['where'][] = ['provinceCode', '=', $post['provinceCode']];
            }
            $res = $this->app->organizationService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($res as $key=>$vo){
                $arr = [];
                $arr['organizationId'] = $vo->organizationId;
                $arr['organizationName'] = $vo->organizationName;
                $arr['categoryId'] = $vo->categoryId;
                $arr['categoryName'] = $vo->getCategoryName();
                $arr['organizationLogo'] = $vo->organizationLogo;
                $arr['provinceCode'] = $vo->provinceCode;
                $arr['provinceName'] = $vo->province->provinceName;
                $arr['personCountView'] = $vo->personCountView;
                $arr['creditCountView'] = $vo->creditCountView;
                $arr['status'] = $vo->status;
                $arr['statusText'] = $vo->getStatusText();
                $arr['createdAt'] = $vo->createdAt;
                $arr['startedAt'] = $vo->startedAt;
                $arr['endAt'] = $vo->endAt;
                $data['lists'][] = $arr;
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($res);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 详情
     * @param  [type] $id
     *
     * @return void
     */
    public function detailAction($id)
    {
        try{
            $organization = $this->app->organizationService->getDetail($id);
            if(!$organization){
                throw new \Exception("商品不存在",500);
            }
            $data = $organization->toArray();

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 添加商品
     * @return \think\response\Json
     */
    public function addAction()
    {
        try{
            if(empty($this->post)){
                throw new \Exception("没有数据提交，不能处理",500);
            }
            $this->post['adminId'] = $this->admin->adminId;
            $this->post['appletId'] = $this->appletId;
            $this->app->organizationService->add($this->post);

            return json(['code' => 200, 'msg' => '添加成功', 'data'=> []]);
        }catch(\Exception $e){
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @Description 名录修改
     *
     * @return void
     */
    public function updateAction($id)
    {
        try {
            $organization = $this->app->organizationService->getDetail($id);

            $this->app->organizationService->update($organization, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function deleteAction($id)
    {
        try {
            $organization = $this->app->organizationService->getDetail($id);

            $this->app->organizationService->delete($organization);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($id)
    {
        try {
            $organization = $this->app->organizationService->getDetail($id);

            $this->app->organizationService->enable($organization);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($id)
    {
        try {
            $organization = $this->app->organizationService->getDetail($id);

            $this->app->organizationService->disable($organization);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * 获取组织捐献名单
     * @return \think\response\Json
     */
    public function donateListsAction($id)
    {
        try {
            $organization = $this->app->organizationService->getDetail($id);


            $param['where'][] = ['organizationId', '=', $organization->organizationId];
            $param['order'] = ['donateCredit' => 'desc'];
            $lists = $this->app->organizationService->getDonateLists($params=[], $page=1, $pagesize=10);

            $data['lists'] = [];
            foreach($lists as $key => $vo){
                $arr = [];
                $arr['donateId'] = $vo->organizationId;
                $arr['organizationId'] = $vo->organizationId;
                $arr['userId'] = $vo->userId;
                $arr['userName'] = $vo->user->userName;
                $arr['headImg'] = $vo->user->headImg;
                $arr['donateCredit'] = $vo->donateCredit;
                $arr['createdAt'] = $vo->createdAt;
                $data['lists'][] = $arr;
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($lists);

            return json(['code' => 200, 'msg' => '查询成功', 'data'=> $data]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }
}